package com.shanzmoo.h5.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shanzmoo.db.personnel.entity.MemberInfoEntity;
import com.shanzmoo.db.personnel.service.IMemberInfoService;
import com.shanzmoo.h5.domain.AuthUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 自定义 UserDetailsService
 * @author: Gzhao 2020/11/9
 */
@Slf4j
@Service
public class AuthUserDetailsService implements UserDetailsService {

    @Autowired
    private IMemberInfoService memberInfoService;

    @Override
    public UserDetails loadUserByUsername(String mobile) throws UsernameNotFoundException {
        log.info("手机号:" + mobile);

        MemberInfoEntity menberInfo = memberInfoService.getOne(
                new QueryWrapper<MemberInfoEntity>()
                        .eq("mobile", mobile)
                        .eq("deleted", false)
        );
        if (menberInfo != null){
            return AuthUser.create(menberInfo, null, null);
        } else {
            menberInfo = new MemberInfoEntity();
            menberInfo.setMobile(mobile);
            return AuthUser.create(menberInfo, null, null);
        }
    }
}
